/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetluc;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

/**
 *
 * @author Lone_Wolf
 */
public class CreerNouvEmploye extends JPanel {

    private Fenetre fenetre;
    Font f = new Font("Times New Roman", Font.BOLD, 24);
    private GridBagLayout gbl;
    private GridBagConstraints gbc;
    private JLabel lblNom;
    private JLabel lblInitiales;
    private JLabel lblSalaire;
    private JLabel lblMotPasse1;
    private JLabel lblMotPasse2;
    private JLabel lblTitre;
    private JLabel lblTache;
    private JLabel lblExisteDeja;
    private JComboBox combTache;
    private JTextField txtNom;
    private JTextField txtInitiales;
    private JTextField txtSalaire;
    private JPasswordField txtMotPasse1;
    private JPasswordField txtMotPasse2;
    private JButton btnCreer;
    private JButton btnAnnuler;
    private Travail_Effectue liste_taches;

    public CreerNouvEmploye(Fenetre fenetre) {
        this.fenetre = fenetre;
        initComposantes();
        buildInterface();

        fenetre.setTitle("Nouvel Employé");
        //fenetre.setSize(400, 430);

        requestFirstFocus();
    }

    private void requestFirstFocus() {
        fenetre.addWindowFocusListener(new WindowAdapter() {

            public void windowGainedFocus(WindowEvent e) {
                txtNom.requestFocusInWindow();
            }
        });
        JFrame trick = new JFrame();
        trick.setVisible(true);
        trick.setVisible(false);
        fenetre.requestFocus();
    }

    private void initComposantes() {
        lblTitre = new JLabel("Nouvel Employé");
        lblTitre.setFont(f);

        lblNom = new JLabel("Entrer le nom de l'employé : ");
        lblInitiales = new JLabel("Entrer les initiales de l'employé : ");
        lblSalaire = new JLabel("Entrer le salaire de l'employé ($/h) : ");
        lblMotPasse1 = new JLabel("Entrer le mot de passe : ");
        lblMotPasse2 = new JLabel("Répéter le mot de passe : ");
        lblTache = new JLabel("Sélectionner la tâche habituelle : ");

        lblExisteDeja = new JLabel("Cet employé existe déjà!");
        lblExisteDeja.setForeground(Color.red);
        lblExisteDeja.setVisible(false);

        txtNom = new JTextField();
        txtInitiales = new JTextField();
        txtSalaire = new JTextField();
        txtMotPasse1 = new JPasswordField();
        txtMotPasse2 = new JPasswordField();
        combTache = new JComboBox(liste_taches.values());

        btnCreer = new JButton("Créer");
        btnAnnuler = new JButton("Annuler");
        btnCreer.addActionListener(new GererEvenements());
        btnAnnuler.addActionListener(new GererEvenements());

        txtNom.addKeyListener(new GererFocus());
        txtInitiales.addKeyListener(new GererFocus());
        txtMotPasse1.addKeyListener(new GererFocus());
        txtMotPasse2.addKeyListener(new GererFocus());
        txtSalaire.addKeyListener(new GererFocus());
        combTache.addKeyListener(new GererFocus());
    }

    private void buildInterface() {
        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();
        this.setLayout(gbl);
        gbc.fill = GridBagConstraints.BOTH;

        makeSeparator();
        makeSeparator();
        makeBorder();
        gbc.weightx = 0.0;
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(lblTitre, gbc);
        this.add(lblTitre);

        makeSeparator();                        //Nom
        makeBorder();
        gbc.weightx = 0.1;
        gbl.setConstraints(lblNom, gbc);
        this.add(lblNom);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(txtNom, gbc);
        this.add(txtNom);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();

        makeSeparator();                        //Initiales
        makeBorder();
        gbc.weightx = 0.1;
        gbl.setConstraints(lblInitiales, gbc);
        this.add(lblInitiales);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(txtInitiales, gbc);
        this.add(txtInitiales);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();

        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;                      //Mot Passe #1
        gbl.setConstraints(lblMotPasse1, gbc);
        this.add(lblMotPasse1);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(txtMotPasse1, gbc);
        this.add(txtMotPasse1);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();

        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;                      //Mot Passe #2
        gbl.setConstraints(lblMotPasse2, gbc);
        this.add(lblMotPasse2);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(txtMotPasse2, gbc);
        this.add(txtMotPasse2);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();

        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;                      //Salaire
        gbl.setConstraints(lblSalaire, gbc);
        this.add(lblSalaire);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(txtSalaire, gbc);
        this.add(txtSalaire);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();

        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;                      //Tache
        gbl.setConstraints(lblTache, gbc);
        this.add(lblTache);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(combTache, gbc);
        this.add(combTache);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();

        makeSeparator();                        //Boutons
        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;
        gbc.fill = gbc.NONE;
        gbl.setConstraints(btnAnnuler, gbc);
        gbl.setConstraints(btnCreer, gbc);
        this.add(btnAnnuler);
        makeBorder();
        gbl.setConstraints(btnCreer, gbc);
        this.add(btnCreer);
        makeBorder();
        makeSeparator();

        makeBorder();
        makeBorder();
        makeBorder();
        gbc.weightx = 0.1;
        gbc.fill = gbc.NONE;
        gbl.setConstraints(lblExisteDeja, gbc);
        this.add(lblExisteDeja);
        makeBorder();
    }

    private void makeSeparator() {
        JLabel sep = new JLabel(" ");
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(sep, gbc);
        this.add(sep);
        gbc.gridwidth = gbc.CENTER;
        gbc.weighty = 0;
    }

    private void makeBorder() {
        gbc.weightx = 0.1;
        JLabel sep = new JLabel(" ");
        gbl.setConstraints(sep, gbc);
        this.add(sep);
        gbc.gridwidth = gbc.CENTER;
    }

    private boolean verifierDonnees() {
        boolean valide = false;
        btnCreer.setForeground(Color.darkGray);
        lblExisteDeja.setVisible(false);

        if (new String(txtMotPasse1.getPassword()).equals(new String(txtMotPasse2.getPassword())) && txtMotPasse1.getPassword().length != 0) {
            lblMotPasse1.setForeground(Color.darkGray);
            lblMotPasse2.setForeground(Color.darkGray);
            if (!txtNom.getText().isEmpty()) {
                lblNom.setForeground(Color.darkGray);
                if (!txtInitiales.getText().isEmpty()) {
                    lblInitiales.setForeground(Color.darkGray);
                    txtInitiales.setText(txtInitiales.getText().toUpperCase());
                    if (!txtSalaire.getText().isEmpty()) {
                        txtSalaire.setForeground(Color.darkGray);
                        if (!alreadyExists()) {
                            valide = true;
                        } else {
                            btnCreer.setForeground(Color.red);
                            lblExisteDeja.setVisible(true);
                        }
                    } else {
                        txtSalaire.setForeground(Color.red);
                        txtSalaire.requestFocusInWindow();
                    }
                } else {
                    lblInitiales.setForeground(Color.red);
                    txtInitiales.requestFocusInWindow();
                }
            } else {
                lblNom.setForeground(Color.red);
                txtNom.requestFocusInWindow();
            }
        } else {
            lblMotPasse1.setForeground(Color.red);
            lblMotPasse2.setForeground(Color.red);
            txtMotPasse1.requestFocusInWindow();
        }

        return valide;
    }

    private boolean alreadyExists() {
        boolean exists = false;
        Employe emTemp;

        for (int i = 0; i < fenetre.arEmploye.size(); i++) {
            emTemp = fenetre.arEmploye.get(i);
            if (emTemp.getNom().equals(txtNom.getText()) || emTemp.getInitiales().equals(txtInitiales.getText())) {
                exists = true;
            }
        }

        return exists;
    }

    class GererEvenements implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == btnCreer) {
                if (verifierDonnees()) {
                    fenetre.arEmploye.add(new Employe(txtNom.getText(),
                            txtInitiales.getText(), new String(txtMotPasse1.getPassword()),
                            Double.parseDouble(txtSalaire.getText()), false,
                            (Travail_Effectue) combTache.getSelectedItem()));
                    try {
                        fenetre.sauvegarderBD();
                        fenetre.AfficherEcranRecherche();
                    } catch (IOException ex) {
                        Logger.getLogger(CreerNouvEmploye.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(CreerNouvClient.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } else if (e.getSource() == btnAnnuler) {
                try {
                    fenetre.AfficherEcranRecherche();
                } catch (IOException ex) {
                    Logger.getLogger(CreerNouvEmploye.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(CreerNouvEmploye.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    class GererFocus implements KeyListener {

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
        }

        /**
         * permet de changer le focus d'un champ de texte (JTextField) à un autre grâce à la touche "Entrée".
         * @param e
         * permet d'itentifier la source dans laquelle une touche a étée relâchée.
         */
        public void keyReleased(KeyEvent e) {
            if (e.getSource() == txtNom) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    txtInitiales.requestFocusInWindow();
                }
            } else if (e.getSource() == txtInitiales) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    txtMotPasse1.requestFocusInWindow();
                }
            } else if (e.getSource() == txtMotPasse1) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    txtMotPasse2.requestFocusInWindow();
                }
            } else if (e.getSource() == txtMotPasse2) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    txtSalaire.requestFocusInWindow();
                }
            } else if (e.getSource() == txtSalaire) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    combTache.requestFocusInWindow();
                }
            } else if (e.getSource() == combTache) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    btnCreer.doClick();
                }
            }
        }
    }
}
